/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */


package groovy.prefuse.factory

import groovy.prefuse.impl.visual.sort.*

import prefuse.visual.sort.*
import prefuse.Display

public class SorterFactory extends AbstractFactory
{
	private Class klass

	public SorterFactory(Class klass)
	{
		this.klass = klass
	}
	
	boolean isLeaf()
	{
		return true
	}
    
    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException
	{
		if(value == null && attributes.sorter)
		{
			value = attributes.remove("sorter")
		}
		
		if(value instanceof ItemSorter)
		{
			return value
		}
		
		def buildClass
		if(klass)
		{
			buildClass = klass
		}
		else if(value instanceof Class)
		{
			buildClass = value
			value = null
		}

		def sorter = null
		
		//a closure to be instantiated in ClosureItemSorter
		if(buildClass instanceof Class && ItemSorter.isAssignableFrom(buildClass))
		{
			if(ItemSorter.equals(buildClass))
			{
				if(attributes.closure)
				{
					sorter = new ClosureItemSorter(attributes.remove("closure"))
				}
				else if(attributes.score)
				{
					sorter = new ClosureItemSorter(attributes.remove("score"))
				}
				else
				{
					sorter = new ItemSorter()
				}
			}
			else if(TreeDepthItemSorter.equals(buildClass))
			{
				def childrenAbove = false
				if(attributes.childrenAbove != null){childrenAbove = attributes.remove("childrenAbove")}
				
				if(attributes.closure)
				{
					sorter = new ClosureTreeDepthItemSorter(attributes.remove("closure"), childrenAbove)
				}
				else if(attributes.score)
				{
					sorter = new ClosureTreeDepthItemSorter(attributes.remove("score"), childrenAbove)
				}
				else
				{
					sorter = new TreeDepthItemSorter(childrenAbove)
				}
			}
			else if(attributes.closure)
			{
				sorter = new ClosureItemSorter(attributes.remove("closure"))
			}
			else if(attributes.score)
			{
				sorter = new ClosureItemSorter(attributes.remove("score"))
			}
			else
			{
				sorter = buildClass.newInstance()
			}
		}
		
		return sorter
	}

	
    public void setParent(FactoryBuilderSupport builder, Object parent, Object child) 
	{
		if(!(parent instanceof Display))
		{
			return
		}
		
		parent.setItemSorter(child)
    }
}
